$(function() {
	layui.use(['form', 'laydate', 'layer'], function() {
		var laydate = layui.laydate;
		var form = layui.form;
		var layer = layui.layer;
		form.render();
		//重新加载date空间
		laydate.render({
			elem: "#birthday", //绑定文本框的id
			type: "date" //时间类型，年月日，时分秒
		});

		//查询后台的角色列表
		function find_roleName_list() {
			$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/role/selectRoleName",
				async: true,
				"success": function(result) {
					if(result.resultCode == 0) {
						build_roleName_List(result);
					} else {
						layer.msg(result.message, {
							icon: 2,
							time: 1000
						});
					}

				}
			});
		}
		//在页面上渲染角色列表
		function build_roleName_List(result) {
			var roleNameList = result.data;
			$.each(roleNameList, function(index, item) {
				var roleCheckBox = $("<input type='checkbox' value='" + item.role_id + "' title='" + item.name + "' >");
				$(".roleList").append(roleCheckBox);
			});
			form.render();
		}
		//调用查询后台角色列表的函数
		find_roleName_list();

		$(".addUser").click(function() {
			 check_role();
			var token=uuid(16,16);
			$("#token").val(token);
		});
		//绑定表单提交事件
		form.on("submit(addUser)", function(data) {
			//所有表单提交，都是通过name去获取当前元素值
			console.log(data.field); //拿到当前form表单所有元素的值
			//$("form").serialize();获取表单的所有值
			var params = data.field;
			add_User(params);
			//防止表单跳转
			return false;
		});

		//遍历角色列表，给选中的角色列表附加name
		function check_role() {
			var i = 0;
			$("input[type='checkbox']").each(function() {
				if($(this).is(":checked")) {
					$(this).attr("name", "listUserRole[" + i + "].fk_role_id");
					i++;
				}

			});
			form.render();
		}
        //向后台添加一条用户记录
		function add_User(params) {
			$.ajax({
				type: "post",
				url: "http://localhost:8080/ssmService/sysUser/insertUserVo",
				data: params,
				async:false,
			     "success":function(result) {
					if(result.resultCode == 0) {
						layer.msg(result.message, {
							icon: 1,
							time: 1000
						});
						
						//获取当前弹出层，通过父页面获取当前frame框的索引，即窗口名称
						var index=parent.layer.getframeIndex(window.name);
						//刷新父页面
						  parent.location.reload();
						  //关闭弹出层。通过父页面关闭当前弹出层
						  parent.layer.close(index);
						/*var index = parent.layer.getFrameIndex(window.name);
						window.parent.location.reload();//刷新父页面
						parent.layer.close(index);//关闭当前页面*/

					} else {
						layer.msg(result.message, {
							icon: 2,
							time: 1000
						});
					}
				},
				"error":function(){
					layer.msg("该账号已存在", {
							icon: 2,
							time: 1000
						});
				}
			});

		}
		
		
		
  function uuid(len, radix) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  var uuid = [], i;
  radix = radix || chars.length;
 
  if (len) {
   // Compact form
   for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
  } else {
   // rfc4122, version 4 form
   var r;
 
   // rfc4122 requires these characters
   uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
   uuid[14] = '4';
 
   // Fill in random data. At i==19 set the high bits of clock sequence as
   // per rfc4122, sec. 4.1.5
   for (i = 0; i < 36; i++) {
    if (!uuid[i]) {
     r = 0 | Math.random()*16;
     uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
    }
   }
  }
 
  return uuid.join('');
}
	});
});